<template>
  <div>
    <!-- 设备管理 -->
    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="设备名称">
          <el-input
            v-model="name"
            placeholder="请输入设备名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="实验室位置">
          <!-- <el-input
            v-model="local"
            placeholder="请输入实验室位置"
          ></el-input> -->
          <el-select v-model="local" placeholder="请选择实验室">
              <el-option
                v-for="item in labors"
                :key="item.id"
                :label="item.name"
                :value="item.local">
              </el-option>
            </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="load(1)">查询</el-button>
          <el-button type="info" @click="reset()">清空</el-button>
        </el-form-item>
      </el-form>

      <!--按钮-->
      <el-row>
        <el-button type="danger" size="medium" @click="deleteByIds"
          >- 批量删除</el-button
        >
        <el-button type="primary" size="medium" @click="insert()">+ 新增设备</el-button>

        <el-upload
              class="upload-demo"
              action="http://localhost:8080/devices/insertBatch"
              :show-file-list= "false"
              :on-success="handFileSuccess"
              style="display: inline-block; margin-left : 10px"
            >
              <el-button type="primary" plain >+ 批量导入实验室设备</el-button>
            </el-upload>
      </el-row>
    </el-card>

    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="设备名称"> </el-table-column>
      <el-table-column prop="local" label="实验室位置"> </el-table-column>
      <el-table-column label="状态" align="center"> 
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.state"
            @change="changeOpen(scope.row)"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>

        </template>
      </el-table-column>

      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
            {{scope.row.createTime ? scope.row.createTime.replace('T',' '):''}}
          </template>
      </el-table-column>

      <el-table-column align="center" label="修改时间">
        <template slot-scope="scope">
            {{scope.row.updateTime ? scope.row.updateTime.replace('T',' '):''}}
          </template>
      </el-table-column>


      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="update(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-row>
      <div class="block" style="margin:10px 0">
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
    </el-row>

   <el-dialog title="实验室设备管理" :visible.sync="formVisible"  width="40%">
        <el-form :model="form" label-width="80px" style="padding-right : 20px" :rules="rules">
            <el-form-item label="实验室设备名" prop="name">
            <el-input v-model="form.name"></el-input>
            </el-form-item>
          
            <el-form-item label="实验室位置" prop="local">
            <!-- <el-input v-model="form.local"></el-input> -->
              <el-select v-model="form.local" placeholder="请选择实验室">
              <el-option
                v-for="item in labors"
                :key="item.id"
                :label="item.name"
                :value="item.local">
              </el-option>
            </el-select>
            
            </el-form-item>

            <el-form-item>
            <el-button type="primary" @click="save()">提交</el-button>
            <el-button @click="formVisible = false">取消</el-button>
            </el-form-item>
      </el-form>
    </el-dialog>

    
  </div>
</template>

<script>
export default {
  name : 'ADevice',
  data () {
    return {
      name: '',
      local: '',
      tableData:[], // 所有数据
      page : 1, // 当前的页码
      pageSize : 10, // 每页显示的个数
      total : 0,
      ids : [],
      formVisible : false,
      form : {},
      labors : [],
      rules: {
          
          name: [
            { required: true, message: '请输入设备', trigger: 'blur' },
            { min: 1, max: 150, message: '长度在 1 到 150 个字符', trigger: 'blur' }
          
          ],
          local : [
            { required: true, message: '请输入实验室位置', trigger: 'blur' }
          ]

      },
      
    }
  },
  created () {
    this.load(1);
    this.loadLabors();
  },
  methods: {
    loadLabors(){
            this.$request.get('/labors/selectAll').then((result) => {
                this.labors = result.data;
            }).catch((err) => {
                console.log(err);
            });
        },
    handFileSuccess(res){
      // console.log(res);
      if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
         this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'error'
          });
          }
    },
    changeOpen(form){
          this.form = JSON.parse(JSON.stringify(form));
          this.save();

      },
    insert(){
        this.form = {};
        this.formVisible = true;
      },
      update(row){ // 编辑
        this.form = JSON.parse(JSON.stringify(row)); // 给form对象赋值
        this.formVisible = true; // 显示弹窗
      },
    save(){ 
          this.$request({
              url : '/devices',
              method : this.form.id ? 'put' : 'post',
              data : this.form
          }).then(res => {
              if(res.code === 1){
                  this.$message({
                  //message: '登陆成功',
                  message:res.msg,
                  type: 'success'
              });
              this.load(1);
              this.formVisible = false;
              }else{
                  this.$message({
                  //message: '登陆失败',
                  message: res.msg,
                  type: 'warning'
              });
              }
          });
      },
    handleCurrentChange(page){
      //  this.page = page;
        this.load(page);
      }, 
      handleSelectionChange(rows) {
          this.ids = rows.map(v => v.id);
      },
      load(page){ // 分页查询
        if (page) {
            this.page = page;
        }
        this.$request.get('/devices',{
            params : {
                page : this.page,
                pageSize : this.pageSize,
                name: this.name,
                local: this.local

            }
        }).then((res) => {
            this.total = res.data.total;
            this.tableData = res.data.rows;
        }).catch((err) => {
            console.log(err);
        });
    },
    reset(){
        this.name = '';
        this.local = '';
        this.load(1);
    },
    deleteById(id){
       this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((result) => {
        console.log(result);
        this.$request.delete('/devices/' + id ).then((res) => {
          if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
          this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'warning'
          });
          }
        }).catch((error) => {
          console.log(error);
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      }).catch((err) => {
        console.log(err);
      });
    },
    deleteByIds(){
      if(!this.ids.length){
        this.$message.warning('请选择数据！');
        return;
      }

      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((result) => {
        console.log(result);
        this.$request.delete('/devices/' + this.ids).then((res) => {
          if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
          this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'warning'
          });
          }
        }).catch((error) => {
          console.log(error);
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      }).catch((err) => {
        console.log(err);
      });
    },


  }

}
</script>

<style>

</style>